import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useAnalysisStore = defineStore('analysis', () => {
  // State
  const currentImage = ref(null)
  const scanType = ref('abdomen') // 'chest' or 'abdomen'
  const vertebralLevel = ref('L3') // 'T12', 'L1', 'L2', 'L3', 'L4', 'L5'
  const analysisResult = ref(null)
  const isAnalyzing = ref(false)
  const analysisProgress = ref(0)
  const displayOptions = ref({
    subcutaneousFat: true,
    visceralFat: true,
    skeletalMuscle: true
  })

  // Actions
  const loadImage = (file) => {
    return new Promise((resolve) => {
      const reader = new FileReader()
      reader.onload = (e) => {
        currentImage.value = {
          file: file,
          url: e.target.result,
          name: file.name,
          size: (file.size / (1024 * 1024)).toFixed(2) + ' MB',
          uploadTime: new Date().toISOString()
        }
        resolve(currentImage.value)
      }
      reader.readAsDataURL(file)
    })
  }

  const setScanType = (type) => {
    scanType.value = type
  }

  const setVertebralLevel = (level) => {
    vertebralLevel.value = level
  }

  const startAnalysis = () => {
    return new Promise((resolve) => {
      isAnalyzing.value = true
      analysisProgress.value = 0

      // 模拟分析进度
      const interval = setInterval(() => {
        analysisProgress.value += 10
        
        if (analysisProgress.value >= 100) {
          clearInterval(interval)
          
          // 模拟分析结果
          analysisResult.value = {
            id: 'R' + Date.now(),
            patientInfo: {
              id: 'P' + Math.floor(Math.random() * 10000).toString().padStart(6, '0'),
              name: '测试患者',
              gender: '男',
              age: 45,
              date: new Date().toISOString().split('T')[0]
            },
            scanInfo: {
              type: scanType.value === 'chest' ? '胸部CT' : '腹部CT',
              level: vertebralLevel.value
            },
            results: {
              subcutaneousFat: (Math.random() * 50 + 100).toFixed(1),
              visceralFat: (Math.random() * 40 + 70).toFixed(1),
              skeletalMuscle: (Math.random() * 50 + 130).toFixed(1),
              vsfRatio: (Math.random() * 0.3 + 0.6).toFixed(2)
            },
            riskAssessment: {
              visceralObesity: ['低', '中等', '高'][Math.floor(Math.random() * 3)],
              sarcopenia: ['低', '中等', '高'][Math.floor(Math.random() * 3)],
              metabolicSyndrome: ['低', '中等', '高'][Math.floor(Math.random() * 3)]
            },
            imageUrl: currentImage.value?.url || '',
            createTime: new Date().toISOString()
          }
          
          isAnalyzing.value = false
          resolve(analysisResult.value)
        }
      }, 300)
    })
  }

  const saveResult = (result) => {
    analysisResult.value = result
  }

  const updateDisplayOptions = (options) => {
    displayOptions.value = { ...displayOptions.value, ...options }
  }

  const resetAnalysis = () => {
    currentImage.value = null
    analysisResult.value = null
    isAnalyzing.value = false
    analysisProgress.value = 0
  }

  return {
    currentImage,
    scanType,
    vertebralLevel,
    analysisResult,
    isAnalyzing,
    analysisProgress,
    displayOptions,
    loadImage,
    setScanType,
    setVertebralLevel,
    startAnalysis,
    saveResult,
    updateDisplayOptions,
    resetAnalysis
  }
})

